
$(function() {
	
	Tip.emp('请假人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("请假人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		}
	});
});

var Vacation = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.emp = form.emp.value;
				body.name = form.name.value;
				body.vacation = form.vacation.value;
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;
				body.hh = form.hh.value;
				body.content = form.content.value;
				
				$(this).dialog("close");

				var url = 'kq.php?do=Vacation.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_dept_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"更新": function() {
				if (!confirm("确认是否开始提交？")) {
					return false;
				}
				
				var form = $("#form_edit")[0];

				var body = { };
				body.t1 = form.t1.value;
				body.t2 = form.t2.value;

				$(this).dialog("close");

				$.post('kq.php?do=Vacation.deptPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},	

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'kq.php?do=Vacation.edit&id=' + id;
		var c2 = 'kq.php?do=Vacation.view&id=' + id;

		var a1 = ['编辑信息', 'url', 'Vacation.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];

		// 申请
		if (status == 1) {
			var d1 = 'kq.php?do=Vacation.verify&id=' + id;
			var b1 = ['审核请假', 'url', 'Vacation.dialog_verify', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}
		
		// 
		if (status == 2) {
			var d1 = 'kq.php?do=Vacation.approve&id=' + id;
			var b1 = ['批准请假', 'url', 'Vacation.dialog_approve', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}
		
		// 有效
		if (status == 2) {
			var s = c1 + ", " + c2;

			return this.popup_(e, [a1, a2], s);
		}
		
		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c2= 'kq.php?do=Vacation.upload&id=' + id;
		var a2 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var c3 = 'kq.php?do=Vacation.copy&id=' + id;
		var a3 = ['复制信息', 'url', 'Vacation.dialog_copy_', '650x410'];
		
		var ss = 'kq.php?do=Vacation.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Vacation.dialog_ss', '400x200'];
		
		s += ", " + c2 + ", " + c3 + ", " + ss;
		
		a.push(a2);
		a.push(a3);
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.field = form.field.value;
				body.data = form.data.value;
				body.status = form.status.value;

				$(this).dialog("close");

				var url = 'kq.php?do=Vacation.ssPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
	
				var body = {};
				body.id = form.id.value;
				body.emp = form.emp.value;
				body.name = form.name.value;
				body.vacation = form.vacation.value;
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;
				body.hh = form.hh.value;
				body.content = form.content.value;
	
				$(this).dialog("close");
	
				var url = 'kq.php?do=Vacation.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
	
				var body = {};
				
				body.emp = form.emp.value;
				body.name = form.name.value;
				body.vacation = form.vacation.value;
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;
				body.hh = form.hh.value;
				body.content = form.content.value;
	
				$(this).dialog("close");
	
				var url = 'kq.php?do=Vacation.addPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	download: function (key) {
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'kq.php?do=Vacation.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'kq.php?do=Vacation.upload&id=' + id;
		
		var url = 'kq.php?do=Vacation.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},

	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('kq.php?do=Vacation.audit&item=43', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否不同意请假？")) {
							return false;
						}				
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 9;
						
						$(this).dialog("close");

						$.post('kq.php?do=Vacation.verifyPost', body, function(data) {
							$(result).html(data);
						});
					},
					
					"同意": function() {
						if (!confirm("确认是否同意请假？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");

						$.post('kq.php?do=Vacation.verifyPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	dialog_approve: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('kq.php?do=Vacation.audit&item=44', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"批准": function() {
						if (!confirm("确认是否批准请假？")) {
							return false;
						}
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 3;
						
						var url = 'kq.php?do=Vacation.approvePost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'kq.php?do=Vacation.delete' + p2;
		var c2 = 'kq.php?do=Vacation.batch_verify';
		var c3 = 'kq.php?do=Vacation.batch_approve';
		
		var c9 = 'kq.php?do=Vacation.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['审核信息', 'url', 'Vacation.dialog_batch_verify', '800x450'];
		var a3 = ['批准请假', 'url', 'Vacation.dialog_batch_approve', '800x450'];
		
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2 + ", " + c3 + ", " + c9;

		return popup(e, [a1, a2, a3, a9], s);
	},
	
	dialog_batch_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('kq.php?do=Vacation.audit&item=43', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_1_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否审核不通过？")) {
							return false;
						}
						
						var cc = Vacation._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;
						body.status = 9;
						
						var url = 'kq.php?do=Vacation.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"同意": function() {
						if (!confirm("确认是否同意请假？")) {
							return false;
						}
						
						var cc = Vacation._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;					
						body.status = 2;
						
						var url = 'kq.php?do=Vacation.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	dialog_batch_approve: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('kq.php?do=Vacation.audit&item=44', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_2_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				$("input[name^='c_3_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"批准": function() {
						if (!confirm("确认是否批准请假？")) {
							return false;
						}
						
						var cc = Vacation._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;
						body.status = 3;
						
						var url = 'kq.php?do=Vacation.batch_approvePost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	_cc: function () {
		var c = new Array();
		$("input[name^='b_']").each(function() {
			if ($(this).attr("checked")) {
				c.push($(this).val());
			}
		});
		
		return c;
	}
};

